﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeManageService
{
    public class WishManager
    {
        static public int nextID;
        static public List<Wish> wishes;
        static public bool inited = false;

        public static int GetNextID()
        {
            int oid = nextID++;
            FileManager.WishNextID = nextID;
            FileManager.Write();
            return oid;
        }

        public static void Initialrize(string userName)
        {
            WishDBManager dbManager = new WishDBManager();
            //初次初始化时 读取nextID
            if (!inited)
            {
                FileManager.Read();
                nextID = FileManager.WishNextID;
                inited = true;

                //如果遇到文件损坏的情况 则进行恢复处理
                //处理耗时可能较长 所以一般情况下仍从文件读取
                if (nextID == 1)
                {
                    List<Wish> allPlans = dbManager.GetAllWish();
                    if (allPlans.Count != 0)
                        nextID = dbManager.GetAllWish().Max(p => p.WishID) + 1;
                }
            }
            wishes = dbManager.GetUserWish(userName);
        }

        public static void AddWish(Wish wish)
        {
            WishDBManager dbManager = new WishDBManager();
            wish.WishID = GetNextID();
			Console.WriteLine(wish.WishID);
			dbManager.addWish(wish);
            wishes.Add(wish);
        }

        public static void DeleteWish(int wishID)
        {
            WishDBManager dbManager = new WishDBManager();
            int index = GetWishIndex(wishID);
            if (index != -1)
            {
                wishes.RemoveAt(index);
				dbManager.deleteWish(wishID);
			}
        }

        public static void ModifyWish(Wish modifiedWish)
        {
            int index = GetWishIndex(modifiedWish.WishID);
            if (index != -1)
            {
                wishes[index] = modifiedWish;
                WishDBManager dbManager = new WishDBManager();
                dbManager.modifyWish(modifiedWish);
            }
        }

        public static bool FinishWish(int wishID)
        {
			UserDBManager userDBManager = new UserDBManager();
			int index = GetWishIndex(wishID);
			if(index != -1)
			{
				User user = userDBManager.GetUser(wishes[index].UserName);
				if(user.Score >= wishes[index].RequiredScore)
				{
					user.Score -= wishes[index].RequiredScore;
					userDBManager.modifyUser(user);
					Turnover turnover = new Turnover();
					turnover.Reason = "完成" + wishes[index].Name + "愿望花费";
					turnover.ScoreChange = -wishes[index].RequiredScore;
					turnover.Time = DateTime.Now;
					turnover.UserName = wishes[index].UserName;
					TurnoverManager.AddTurnover(turnover);
					WishDBManager wishDBManager = new WishDBManager();
					wishDBManager.deleteWish(wishID);
					wishes.RemoveAt(index);
					return true;
				}
			}
			return false;
		}

        public static int GetWishIndex(int wishID)
        {
            for (int i = 0; i < wishes.Count; i++)
            {
                if (wishes[i].WishID == wishID)
                    return i;
            }
            return -1;
        }
		
		public static Wish GetWishClone(int wishID)
		{
			int index = GetWishIndex(wishID);
			if (index != -1) return new Wish(wishes[index]);
			else return null;
		}
	}
}
